package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.PageResult;
import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.model.Building;
import com.lonty.intelligence.request.BuildingList;
import com.lonty.intelligence.service.BuildingService;
import com.lonty.intelligence.vo.BuildingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/4/17 13:20
 * @Version 1.0
 */
@RestController
@RequestMapping("app/building")
@Validated
@Api(tags = "楼栋接口", produces = "application/json")
public class BuildingController {

    @Resource
    private BuildingService buildingService;

    @ApiOperation(value = "保存楼栋", httpMethod = "POST")
    @PostMapping("saveBuilding")
    public Result saveBuilding(@ModelAttribute BuildingVo buildingVo){
        Building building = buildingService.saveBuilding(buildingVo);
        if(building != null){
            return Result.builder().data(building).build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "分页获取楼栋", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "小区ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "page", value = "当前页",dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "每页显示的数量",dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("listBuilding")
    public PageResult listBuilding(String areaId, Integer page, Integer size){
        BuildingList buildingList = buildingService.listBuilding(areaId, page, size);
        return PageResult.<Building>builder()
                .data(buildingList.getList())
                .page(buildingList.getPage())
                .total(buildingList.getTotal())
                .build();
    }

    @ApiOperation(value = "根据小区ID获取楼栋并且分组", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "小区ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("getBuildingByAreaId")
    public Result getBuildingByAreaId(String areaId){
        return Result.builder()
                .data(buildingService.getBuildingByAreaId(areaId))
                .build();
    }

    @ApiOperation(value = "判断楼栋是否存在", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "小区ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "road", value = "道路",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "buildingNumber", value = "楼栋号",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("buildingIsExit")
    public Result buildingIsExit(String areaId, String road, String buildingNumber){
        List<Building> building = buildingService.buildingIsExit(areaId, road, buildingNumber);
        return Result.builder().data(building).build();
    }

    @ApiOperation(value = "修改楼栋", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "楼栋ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "住房类型(0:自住 1:租住 2:无人居住)",dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("modifyBuilding")
    public Result modifyBuilding(String id, Integer type){
        if(buildingService.modifyBuilding(id, type)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

}
